package com.jy.adscm.operation.lxcg.controller;


import com.baomidou.mybatisplus.plugins.Page;
import com.jy.adscm.exception.AdscmSystemException;
import com.jy.adscm.operation.cg.bean.CgTask;
import com.jy.adscm.operation.cg.bean.QueryCgTask;
import com.jy.adscm.operation.cg.service.CgTaskService;
import com.jy.adscm.operation.lxcg.bean.LxcgTask;
import com.jy.adscm.operation.lxcg.bean.QueryLxcgTask;
import com.jy.adscm.operation.lxcg.service.LxcgTaskService;
import com.jy.adscm.security.utils.Token;
import com.jy.adscm.support.SimpleCoreResponse;
import com.jy.adscm.utils.AdscmBaseUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;

/**
 * <p>
 * 零星采购任务 前端控制器
 * </p>
 *
 * @author zyl
 * @since 2019-08-15
 */
@RestController
@RequestMapping("/adscm/operation/lxcg/lxcgTask")
public class LxcgTaskController {

    @Autowired
    RedisTemplate<Object, Token> tokenRedisTemplate;
    @Autowired
    LxcgTaskService lxcgTaskService;
    
    
    @RequestMapping("insert")
    public SimpleCoreResponse insert(@RequestBody LxcgTask lxcgTask, HttpServletRequest request)throws AdscmSystemException {
        String tokenId = request.getParameter("tokenId");
        Token token = AdscmBaseUtils.getInstance().getTokenByTokenId(tokenId, tokenRedisTemplate);
        if(!AdscmBaseUtils.getInstance().isNotEmpty(token,null)){
            throw  new AdscmSystemException("token 是空的");
        }

        boolean insert = lxcgTaskService.insertLxcgTask(lxcgTask,token);
        return  new SimpleCoreResponse("200","新增成功",insert);
    }

    @RequestMapping("update")
    public  SimpleCoreResponse  update(@RequestBody LxcgTask lxcgTask, HttpServletRequest request) throws AdscmSystemException {
        String tokenId = request.getParameter("tokenId");
        Token token = AdscmBaseUtils.getInstance().getTokenByTokenId(tokenId, tokenRedisTemplate);
        lxcgTask.setCreator(token.getUser().getId().toString());
        lxcgTask.setCreateTime(new Date());
        boolean result = lxcgTaskService.updateLxcgTask(lxcgTask,token);
        return  new SimpleCoreResponse("200","修改成功",result);
    }
    @RequestMapping("select")
    public  SimpleCoreResponse  select(@RequestBody QueryLxcgTask queryLxcgTask, HttpServletRequest request) throws AdscmSystemException {
        String tokenId = request.getParameter("tokenId");
        Token token = AdscmBaseUtils.getInstance().getTokenByTokenId(tokenId, tokenRedisTemplate);
        Page<LxcgTask> page=new Page<>(queryLxcgTask.getCurrentPage(),queryLxcgTask.getPageSize());
        page= lxcgTaskService.selectLxcgTask(page,queryLxcgTask, token);
        return  new SimpleCoreResponse("200","查询成功",page);
    }


}

